﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace Red.Controls.Converters
{
    /// <summary>
    /// Allows you to chain together multiple converters
    /// </summary>
    /// <remarks>
    /// Shamelessly stolen from http://stackoverflow.com/a/8326207/1683284
    /// </remarks>
    public sealed class ConverterGroup : List<IValueConverter>, IValueConverter
    {
        #region IValueConverter implementation

        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return this.Aggregate( value, ( current, converter ) => converter.Convert( current, targetType, parameter, culture ) );
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            return Enumerable.Reverse( this ).Aggregate( value, ( current, converter ) => converter.ConvertBack( current, targetType, parameter, culture ) );
        }

        #endregion IValueConverter implementation
    }
}
